<template>
  <PageCard title="GitHub 加速工具">
    <div v-if="featureDisabled">
      <FeatureUnavailable>
        因不可抗力原因，该功能暂时下架。
      </FeatureUnavailable>
    </div>

    <div v-else>
      <n-spin :show="loading">
        <n-scrollbar class="max-h-[70vh]">
          <n-space vertical size="large">
            <n-input
                v-model:value="githubAddress"
                type="text"
                size="large"
                placeholder="请输入 GitHub 仓库地址"
                clearable
            />
            <n-space>
              <n-button secondary type="primary" @click="parseAddress(githubAddress)" :loading="loading">解析</n-button>
              <n-button type="primary" @click="download(githubAddress)" :loading="loading">下载 (默认分支)</n-button>
            </n-space>
            <n-alert v-if="downloadUrl" title="提示" type="info" closable>
              点击下载没反应? 请右键复制链接或点击手动下载:
              <n-button
                  text
                  tag="a"
                  :href="downloadUrl"
                  target="_blank"
                  type="primary"
              >
                {{ downloadUrl }}
              </n-button>
            </n-alert>

            <n-divider/>

            <n-gradient-text type="info" :size="20">
              解析结果：
            </n-gradient-text>
            <div>
              <n-space vertical>
                <n-text :type="result === '解析中' ? 'info' : 'error'">
                  {{ result }}
                </n-text>
                <template v-if="showRepositoryInfo(githubAddressType as GithubAddressTypeEnum)">
                  <n-text>默认分支：{{ defaultBranch }}</n-text>
                  <n-text>作者：{{ author }}</n-text>
                  <n-text>仓库名称：{{ repositoryName }}</n-text>
                </template>
              </n-space>
            </div>

            <div v-if="branchList && branchList.length > 0">
              <n-divider/>
              <n-gradient-text type="primary" :size="18">已为您解析出下列分支:</n-gradient-text>
              <n-space vertical class="mt-2.5">
                <n-button
                    v-for="item in branchList"
                    :key="item.branchName"
                    :type="item.defaultFlag === 1 ? 'primary' : 'default'"
                    secondary
                    @click="download(githubAddress, item.branchName)"
                >
                  点此下载 {{ item.branchName }} 分支
                  <template #icon>
                    <n-icon v-if="item.defaultFlag === 1">
                      <FavoriteFilled/>
                    </n-icon>
                  </template>
                </n-button>
              </n-space>
            </div>
          </n-space>
        </n-scrollbar>
      </n-spin>
    </div>
  </PageCard>
</template>

<script setup lang="ts">
import {ref} from 'vue';
import {requestDownloadZip, requestGetBranch, requestInfo} from "@/api/develop-tool/github-util/index.ts";
import {FavoriteFilled} from '@vicons/material';
import {GithubAddressTypeEnum} from '@/views/develop-tool/enums.ts';
import FeatureUnavailable from "@/components/FeatureUnavailable.vue";
import PageCard from "@/components/PageCard.vue";

const featureDisabled = ref(true);
const loading = ref(false);
const githubAddress = ref('https://github.com/07akioni/xicons.git');
const result = ref('未解析');
const downloadUrl = ref('');
const githubAddressType = ref<GithubAddressTypeEnum>();
const defaultBranch = ref<string>();
const author = ref<string>();
const repositoryName = ref<string>();
const branchList = ref<any[]>([]);

const showRepositoryInfo = (type: GithubAddressTypeEnum) => {
  return type && [
    GithubAddressTypeEnum.HAS_BRANCH_REPOSITORY,
    GithubAddressTypeEnum.NO_BRANCH_REPOSITORY
  ].includes(type);
};

const parseAddress = (value: string) => {
  branchList.value = [];
  downloadUrl.value = '';
  defaultBranch.value = '';
  repositoryName.value = '';
  author.value = '';
  githubAddressType.value = undefined;
  result.value = '解析中';
  loading.value = true;

  requestInfo(value).then((res: any) => {
    if (res.data.githubAddressType === 'noBranchRepository') {
      result.value = '解析成功';
      defaultBranch.value = res.data.branchName;
      repositoryName.value = res.data.repositoryName;
      author.value = res.data.author;
      githubAddressType.value = res.data.githubAddressType as GithubAddressTypeEnum;
      return requestGetBranch(value);
    } else {
      result.value = res.data.githubAddressType === 'error' ? '该链接不是有效的 GitHub 仓库' : '暂时不支持该类型的链接';
      throw new Error(result.value);
    }
  }).then((res: any) => {
    if (res) {
      branchList.value = res.data;
    }
  }).catch((err: Error) => {
    console.error(err);
  }).finally(() => {
    loading.value = false;
  });
};

const download = (address: string, branchName?: string) => {
  loading.value = true;
  downloadUrl.value = '';
  requestDownloadZip(address, branchName).then((res: any) => {
    window.open(res.data);
    downloadUrl.value = res.data;
  }).catch((err: Error) => {
    console.error(err);
    // You might want to show an alert here as well
  }).finally(() => {
    loading.value = false;
  });
};
</script>

<style scoped>
</style>
